export function namespace(ns) {
  return function (v) {
    return ns + v;
  };
}

export const sleep = (time) => {
  return new Promise((resolve) => setTimeout(resolve, time));
};

export const ObjectToArr = (obj) => {
  let arr = [];
  Object.keys(obj).forEach((k) => {
    arr.push({
      label: obj[k],
      value: k,
    });
  });
  return arr;
};

export const ArrToObject = (arr) => {
  let obj = {};
  arr.forEach((item) => {
    obj[item.value] = item.label;
  });
  return obj;
};

export const downloadFile = (data, name) => {
  // 根据二进制流创建文件
  // application_vnd.openxmlformats - officedocument.spreadsheetml.sheet
  // charset = utf - 8

  const fileName = name;
  if (window.navigator.msSaveOrOpenBlob) {
    navigator.msSaveBlob(data, fileName);
  } else {
    const link = document.createElement("a");
    link.href = window.URL.createObjectURL(data);
    link.download = fileName;
    link.click();
    window.URL.revokeObjectURL(link.href);
  }
};
